package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC14 {
    public String longestCommonPrefix(String[] strs) {
        if (null == strs || strs.length <= 0) {
            return "";
        }

        if (strs.length == 1) {
            return strs[0];
        }

        StringBuilder sb = new StringBuilder();
        char curChar;
        for (int i = 0; i < strs[0].length(); ++i) {
            curChar = strs[0].charAt(i);
            boolean curMatch = true;
            for (int j = 1; j < strs.length; ++j) {
                if (i > strs[j].length() - 1 || curChar != strs[j].charAt(i)) {
                    curMatch = false;
                    break;
                }
            }
            if (curMatch) {
                sb.append(curChar);
            } else {
                break;
            }
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        LC14 an = new LC14();

        String[] strs = {"flower", "flow", "flight"};
        System.out.println(an.longestCommonPrefix(strs));
    }
}
